﻿using System;
using System.Collections.Generic;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.Threading.Tasks;
using System.IO;
using IronPython.Compiler;
using System.Windows.Threading;
using IronPython.Runtime.Exceptions;

           
using System.ComponentModel; 
 
namespace WpfXamlPopup
{
    public class poizvedbe : debug
    {
        ScriptEngine engine;
        ScriptScope scope;

        //string file1 = Environment.CurrentDirectory + @"..\..\..\..\PythonApplication1\\asysaba04.py";
        //string file2 = Environment.CurrentDirectory + @"\\asysaba04.py";
        //string file1 = Environment.CurrentDirectory + @"..\..\..\..\PythonApplication1\\PythonApplication1.py";
        //string file2 = Environment.CurrentDirectory + @"\\PythonApplication1.py"; 

        debug Debugwindow;
        System.Threading.Thread skripte;
            
        // Konstruktor
        public poizvedbe(debug debugwindow, MainNotifyWindow window) 
        {
            try 
            {


                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                  
                Dictionary<string, object> options = new Dictionary<string, object>();
                options["Debug"] = true;

                engine = Python.CreateEngine(options); // ScriptEng ine - DEBUG 
                //engine = Python.CreateEngine(); // ScriptEngine - no DEBUG
                 
                scope = engine.CreateScope();   // ScriptScope
                Debugwindow = debugwindow;
                
                ScriptRuntime runtime = engine.Runtime;
                runtime.LoadAssembly(typeof(String).Assembly);
                runtime.LoadAssembly(typeof(Uri).Assembly); 
                   
                            
                         
                Console.SetOut(new TextBoxOutputter(debugwindow));
                Console.SetError(new TextBoxOutputterErr(debugwindow));
                runtime.IO.RedirectToConsole();

                scope.SetVariable("obrazec", window);
                scope.SetVariable("debug", debugwindow);

                //BackgroundWorker work = new BackgroundWorker();
                //work.DoWork += new DoWorkEventHandler(work_DoWork);
                //work.RunWorkerAsync();
                work_DoWork(null, null);
                   
                       
            }  
            catch (FileNotFoundException ex)
            {
                debugwindow.TraceDbg("Skripte ne najdem!", ex.Message, MsgType.Error);
            } 
                 
            catch (Exception ex) 
            {
                 
                ExceptionOperations eo;
                eo = engine.GetService<ExceptionOperations>();
                string error = eo.FormatException(ex);
                debugwindow.TraceDbg("Napaka pri odpiranju skripte!", error, MsgType.Error);
            }
              
        }
                 
             
              

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Debugwindow.TraceDbg("Skripta se je sesula - unhandled exceprion!", e.ExceptionObject.ToString(), MsgType.Error);
        }
          
        void work_DoWork(object sender, DoWorkEventArgs e) 
        { 
           try  
            {  
               
                string file;    
                if (File.Exists(spremnilist.Properties.Settings.Default.python_path + spremnilist.Properties.Settings.Default.python_file))
                    file = spremnilist.Properties.Settings.Default.python_path + spremnilist.Properties.Settings.Default.python_file;
                else
                    file = Environment.CurrentDirectory + "\\" + spremnilist.Properties.Settings.Default.python_file;

                ScriptSource source = engine.CreateScriptSourceFromFile(file);
                source.Execute(scope);
            }
            catch (IronPython.Runtime.Exceptions.RuntimeException ex)
            {
                ExceptionOperations eo;
                eo = engine.GetService<ExceptionOperations>();
                string error = eo.FormatException(ex);
                Debugwindow.TraceDbg("Napaka v skripti!", error, MsgType.Error);
            }
            catch (Exception ex)
            {
                ExceptionOperations eo;    
                eo = engine.GetService<ExceptionOperations>();
                string error = eo.FormatException(ex);
                Debugwindow.TraceDbg("Napaka v skripti!", error, MsgType.Error);
                
            }
        }  
          
                     
    }      
} 
           